package cn.gson.crm.controller.student;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.Jdbc;
import cn.gson.crm.model.dao.GraduateDao;
import cn.gson.crm.model.entity.Graduate;

/**
 * 添加就业信息
 */
@WebServlet("/student/addcompanyinf")
public class AddCompanyInfServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private GraduateDao graduateDao = new GraduateDao();
	String id = "";
	Graduate graduate = null;
	String address = "";
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		id = request.getParameter("id");
		graduate = graduateDao.getOneGraduate(id);
		request.setAttribute("graduate", graduate);
		String sql = "select * from province";
		List<Map<String, Object>> data = Jdbc.getInstace().executeQueryForList(sql);
		request.setAttribute("provinces", data);
		forward("student/changecompanyform.jsp");
		// redirect("student/graduate");
	}

	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Date date = null;
		String salary = request.getParameter("salary");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String area = request.getParameter("area");
		String company = request.getParameter("company");
		String time = request.getParameter("time");
		System.out.println(time + "获取的时间");
		// 验证
		if (province == null || province.isEmpty()) {
			address = graduate.getGradWorkplace();
		} else {
			String sqlprovince = "select * from province where id=?";
			String sqlcity = "select * from city where id=?";
			String sqlarea = "select * from area where id=?";
			Map<String, Object> dataprovince = Jdbc.getInstace().executeQuery(sqlprovince, province);
			Map<String, Object> datacity = Jdbc.getInstace().executeQuery(sqlcity, city);
			Map<String, Object> dataarea = Jdbc.getInstace().executeQuery(sqlarea, area);
			if (dataprovince != null && datacity != null && dataarea != null) {
				address = dataprovince.get("province") + "" + datacity.get("city") + "" + dataarea.get("area");
			}
		}
		//验证薪水
		if (salary == null || salary.isEmpty()) {
			salary = graduate.getGradSalary();
		}
		//验证公司
		if (company == null || company.isEmpty()) {
			company = graduate.getGradCompany();
		}
		//验证入职时间
		if (time == null || time.isEmpty()) {
			date = graduate.getGradJobTime();
		}else{
			date=sdf.parse(time);
		}
		//没有公司就没有薪水
		if (company == null || company.isEmpty()) {
			salary = null;
		}
		boolean result = graduateDao.updateGraduateById(id, date,address, company, salary);
		if (!result) {
			errorForward("修改失败", "student/changecompanyform.jsp");
		}
		redirect("student/graduate");
	}

}
